package com.lalilu.homework3.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.lalilu.homework3.R;

public class CustomDialogFragment extends DialogFragment {
    private NotifyDialogFragment listener;

    public void setListener(NotifyDialogFragment listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.custom_dialog_fragment, null);
        EditText username = view.findViewById(R.id.editText_username);
        EditText password = view.findViewById(R.id.editText_password);

        return builder.setView(view)
                .setTitle("自定义对话框")
                .setPositiveButton("确定", (dialog, which) -> {
                    String result = "username: " + username.getText().toString()
                            + "\npassword: " + password.getText().toString();
                    this.listener.onDialogPositiveClicked(result);
                })
                .setNegativeButton("取消", (dialog, which) -> dialog.cancel())
                .create();
    }
}
